<template>
  <div class='person'>
    <!-- 搜索框 -->
    <el-card class="search">
      <el-form ref="form"
               :inline="true"
               class="searchForm"
               :model="form"
               label-width="80px">
        <el-form-item>
          <span>用户ID/昵称</span>
          <el-input v-model="form.name"
                    style="width:60%"></el-input>
        </el-form-item>
        <el-form-item label="日期:">
          <el-col :span="12">
            <el-date-picker disabled
                            type="date"
                            placeholder="选择日期"
                            v-model="form.date1"
                            style="width: 100%;"></el-date-picker>
          </el-col>

          <el-col :span="1">至</el-col>
          <el-col :span="11">
            <el-date-picker disabled
                            type="date"
                            placeholder="选择日期"
                            v-model="form.date2"
                            style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button @click="onSubmit">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 用户数据区域 -->
    <el-card class="user">
      <span>用户数量:{{ this.userConst }}</span>
      <span>订单量:{{ this.orderConst }}</span>
      <span>消费总额:{{ this.expenserConst }}</span>
    </el-card>
    <!-- 数据表单 -->
    <el-card class="form">
      <el-table :data="list"
                style="width: 100%"
                @row-click="tabBtn">
        <el-table-column prop="user_id"
                         label="用户ID">
        </el-table-column>
        <el-table-column prop="user_name"
                         label="用户昵称">
        </el-table-column>
        <el-table-column prop="phone"
                         label="联系电话">
        </el-table-column>
        <el-table-column prop="Bus_count"
                         label="商圈店铺数量">
        </el-table-column>
        <el-table-column prop="order_count"
                         label="订单量">
        </el-table-column>
        <el-table-column prop="newexpenditure"
                         label="消费额">
        </el-table-column>
        <el-table-column prop="neworderConst"
                         label="消费频次">
        </el-table-column>
        <el-table-column prop="address"
                         label="操作">
          <a href="javascript:;"
             @click="aBut">消费分析</a>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        name: '',
        date1: '2020-02-01 ',
        date2: new Date()
      },
      list: [],
      list1: [],
      userConst: 0,
      orderConst: 0,
      expenserConst: 0,
      newObj: {}
    }
  },
  created () {
    this.getlist()
  },
  mounted () { },
  methods: {
    async getlist () {
      const { data } = await this.$http.get('/share')
      this.list = data.list
      data.list.forEach((item, index) => {
        this.orderConst += item.order_count
        this.expenserConst += item.expenditure
        item.newexpenditure = item.expenditure + '元'
        item.neworderConst = item.order_count + '次/天'
      })
      this.userConst = data.list.length

      this.list1 = data.list
    },
    tabBtn (val) {
      this.newObj = val
    },
    // a标签跳转
    aBut () {
      setTimeout(() => {
        this.$router.push({
          name: 'PbMan',
          params: {
            id: this.newObj.user_id,
            phone: this.newObj.phone,
            name: this.newObj.user_name,
            num: this.newObj.expenditure
          }
        })
      }, 0.1)
    },
    onSubmit () {
      this.list.forEach((item, index) => {
        const arr = []
        if (this.form.name) {
          if (
            item.user_id === this.form.name * 1 ||
            item.user_name === this.form.name
          ) {
            arr.push(item)
            this.list = arr
          }
        } else {
          return false
        }
      })
    },
    reset () {
      this.form.name = ''
      this.list = this.list1
    }

  }
}
</script>

<style scoped lang="less">
.search {
  padding: 20px 0 0 0;
  margin-bottom: 15px;
  .searchForm {
    display: flex;
    justify-content: left;
    align-content: center;
  }
}
.user {
  height: 50px;
  margin-bottom: 15px;
  span {
    margin: 20px;
  }
}
.form {
  padding: 5px 15px;
  margin-bottom: 15px;
  .el-table {
    font-size: 12px;
  }
}
</style>
